using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Interop;
using System.IO;

namespace MKDS_Course_Editor.NSBTP
{
    public partial class Wizard : Form
    {
        public Wizard()
        {
            InitializeComponent();
        }
        LibNDSFormats.NSBMD.Nsbmd nsbm;
        NSBTX.nsbtx_.NSBTX_File n;
        string nsbtp;
        private void Wizard_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && openFileDialog1.FileName.Length > 0)
            {
                textBox1.Text = openFileDialog1.FileName;
                nsbm = LibNDSFormats.NSBMD.Nsbmd.FromStream(File.OpenRead(openFileDialog1.FileName));
                if (nsbm.materials == null)
                {
                    textBox2.Visible = true;
                    button2.Visible = true;
                    label2.Visible = true;
                }
                else
                {
                    textBox2.Text = openFileDialog1.FileName;
                    textBox2.Visible = false;
                    button2.Visible = false;
                    label2.Visible = false;
                    nsbm.MatchTextures();
                    NSBTX.nsbtx_ nn = new NSBTX.nsbtx_(openFileDialog1.FileName, false);
                    nn.load();
                    n = nn.nsbtx;
                }
                if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "")
                {
                    wizardPage2.AllowNext = true;
                }
                else
                {
                    wizardPage2.AllowNext = false;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (openFileDialog2.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && openFileDialog2.FileName.Length > 0)
            {
                textBox2.Text = openFileDialog2.FileName;
                nsbm.materials = LibNDSFormats.NSBTX.NsbtxLoader.LoadNsbtx(File.OpenRead(openFileDialog2.FileName),out nsbm.Textures,out nsbm.Palettes);
                if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "")
                {
                    wizardPage2.AllowNext = true;
                }
                else
                {
                    wizardPage2.AllowNext = false;
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK
                && saveFileDialog1.FileName.Length > 0)
            {
                textBox3.Text = saveFileDialog1.FileName;
                nsbtp = saveFileDialog1.FileName;
                if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "")
                {
                    wizardPage2.AllowNext = true;
                }
                else
                {
                    wizardPage2.AllowNext = false;
                }
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedIndices.Count != 0)
            {
                button4.Enabled = true;
            }
            else
            {
                button4.Enabled = false;
            }
        }

        private void listView2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView2.SelectedIndices.Count != 0)
            {
                button5.Enabled = true;
            }
            else
            {
                button5.Enabled = false;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < listView1.SelectedIndices.Count; i++)
            {
                listView2.Items.Add((ListViewItem)listView1.Items[listView1.SelectedIndices[i]].Clone());
                listView1.Items.Remove(listView1.Items[listView1.SelectedIndices[i]]);
            }
            listView2.Sort();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < listView2.SelectedIndices.Count; i++)
            {
                listView1.Items.Add((ListViewItem)listView2.Items[listView2.SelectedIndices[i]].Clone());
                listView2.Items.Remove(listView2.Items[listView2.SelectedIndices[i]]);
            }
            listView1.Sort();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            while (listView1.Items.Count != 0)
            {
                listView2.Items.Add((ListViewItem)listView1.Items[0].Clone());
                listView1.Items.Remove(listView1.Items[0]);
            }
            listView2.Sort();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            while(listView2.Items.Count != 0)
            {
                listView1.Items.Add((ListViewItem)listView2.Items[0].Clone());
                listView2.Items.Remove(listView2.Items[0]);
            }
            listView1.Sort();
        }

        private void wizardPage3_Initialize(object sender, AeroWizard.WizardPageInitEventArgs e)
        {
            for (int i = 0; i < nsbm.models[0].Materials.Count-1; i++)
            {
                ListViewItem it = new ListViewItem(nsbm.models[0].Materials[i].MaterialName);
                it.Tag = i;
                listView1.Items.Add(it);
            }
        }

        private void wizardControl1_Finished(object sender, EventArgs e)
        {
            NSBTP.NSBTP_File tp = new NSBTP.NSBTP_File();
            tp.MPT.names = new string[listView2.Items.Count];
            tp.MPT.infoBlock.Data = new NSBTP.NSBTP_File.M_PT.Info.info[listView2.Items.Count];
            tp.AnimData = new NSBTP.NSBTP_File.animData[listView2.Items.Count];
            nsbm.MatchTextures();
            foreach (ListViewItem s in listView2.Items)
            {
                tp.MPT.names[s.Index] = s.Text;
                tp.MPT.infoBlock.Data[s.Index].KeyFrames = 1;
                tp.MPT.infoBlock.Data[s.Index].Unknown1 = 4096;
                tp.AnimData[s.Index].KeyFrames = new NSBTP.NSBTP_File.animData.keyFrame[1];
                tp.AnimData[s.Index].KeyFrames[0].Start = 0;
                tp.AnimData[s.Index].KeyFrames[0].texId = 0;
                tp.AnimData[s.Index].KeyFrames[0].texName = nsbm.models[0].Materials[(int)s.Tag].texname;
                tp.AnimData[s.Index].KeyFrames[0].palId = 0;
                tp.AnimData[s.Index].KeyFrames[0].palName = nsbm.models[0].Materials[(int)s.Tag].palname;
            }
            tp.MPT.NoTex = (byte)listView2.Items.Count;
            tp.MPT.NoPal = (byte)listView2.Items.Count;
            tp.MPT.NoFrames = 1;
            tp.PAT0.names = new string[1];
            tp.PAT0.names[0] = nsbm.models[0].Name;
            NSBPT_Edit pp = new NSBPT_Edit(tp, textBox2.Text, saveFileDialog1.FileName);
            this.Hide();
            pp.ShowDialog(Application.OpenForms[0]);
        }


    }
}
